<template>
  <div>
    <el-card :bordered="false" shadow="never" class="ivu-mt" :body-style="{padding:0}">
      <div class="padding-add">
        <el-form
            ref="levelFrom"
            :model="bmiFrom"
            :label-width="labelWidth"
            :label-position="labelPosition"
            inline
            @submit.native.prevent
        >

          <el-form-item label="关键词：" label-for="title">
            <el-input
                clearable
                v-model="bmiFrom.title"
                placeholder="用户名/ID/手机号"
                class="form_content_width"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="userSearchs">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>

    <el-card :bordered="false" shadow="never" class="ivu-mt mt16">
      <el-table
        :data="bmiLists"
        ref="table"
        class="mt14"
        v-loading="loading"
        highlight-current-row
        no-userFrom-text="暂无数据"
        no-filtered-userFrom-text="暂无筛选结果"
      >
        <el-table-column label="ID" width="80">
          <template slot-scope="scope">
            <span>{{ scope.row.id }}</span>
          </template>
        </el-table-column>
        <el-table-column label="用户头像/用户名" min-width="150">
          <template slot-scope="scope">
            <div class="avatar">
              <span >{{ scope.row.nickname }}</span> <img  :src="scope.row.avatar" alt="" />
            </div>
          </template>
        </el-table-column>
        <el-table-column label="身高/年龄/性别" width="180" prop="height" >
          <template slot-scope="scope">
           {{scope.row.height }}cm/{{scope.row.m_age}}岁/{{ scope.row.m_gender === 1 ? '男' : scope.row.m_gender === 0 ? '保密': '女' }}
          </template>
        </el-table-column>
        <el-table-column label="电阻" prop="dianzu" width="100" />
        <el-table-column label="体重" prop="dianzu" width="100" />
        <el-table-column label="体脂" prop="tizhi" width="100" />
        <el-table-column label="肌肉" prop="jirou" width="100" />
        <el-table-column label="BMI" prop="bmi" width="100" />
        <el-table-column label="气酮" prop="qitong" width="100" />
        <el-table-column label="称重时间" min-width="130">
          <template slot-scope="scope">
            <span>{{ scope.row.add_time | formatDate }}</span>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <a @click="del(scope.row, '删除数据', scope.$index)">删除</a>
          </template>
        </el-table-column>
      </el-table>
      <div class="acea-row row-right page">
        <pagination
          v-if="total"
          :total="total"
          :page.sync="bmiFrom.page"
          :limit.sync="bmiFrom.limit"
          @pagination="getList"
        />
      </div>
    </el-card>
  </div>
</template>

<script>
import { mapState } from 'vuex';
import { userBmiList } from '@/api/user';
import {formatDate} from "@/utils/validate";
export default {
  name: 'bmi_list',
  data() {
    return {

      loading: false,
      bmiLists: [],
      total: 0,
      bmiFrom:{
        title: '',
        page: 1,
        limit: 15,
      }
    };
  },
  computed: {
    ...mapState('media', ['isMobile']),
    labelWidth() {
      return this.isMobile ? undefined : '80px';
    },
    labelPosition() {
      return this.isMobile ? 'top' : 'left';
    },
  },
  filters: {
    formatDate(time) {
      if (time !== 0) {
        let date = new Date(time * 1000);
        return formatDate(date, 'yyyy-MM-dd hh:mm');
      }
    },
  },
  created() {
    this.getList();
  },
  methods: {
    // 表格搜索
    userSearchs() {
      this.bmiFrom.page = 1;
      this.getList();
    },
    // 分组列表
    getList() {
      this.loading = true;
      userBmiList(this.bmiFrom)
        .then(async (res) => {
          let data = res.data;
          this.bmiLists = data.list;
          this.total = data.count;
          this.loading = false;
        })
        .catch((res) => {
          this.loading = false;
          this.$message.error(res.msg);
        });
    },
    // 删除
    del(row, tit, num) {
      // let delfromData = {
      //   title: tit,
      //   num: num,
      //   url: `user/user_bmi/del?id=${row.id}`,
      //   method: 'DELETE',
      //   ids: '',
      // };
	  let delfromData = {
	    title: tit,
	    num: num,
	    url: `user/user_bmi/del/${row.id}`,
	    method: 'DELETE',
	    ids: '',
	  };
      this.$modalSure(delfromData)
        .then((res) => {
          this.$message.success(res.msg);
          this.bmiLists.splice(num, 1);
          this.getList();
        })
        .catch((res) => {
          this.$message.error(res.msg);
        });
    },
  },
};
</script>

<style scoped lang="stylus">
.avatar{
  display inline-flex;
  align-items center;
  span{
    padding-right 8px
  }
  img{
    width :40px;
    height: 40px;
    border-radius 50%
  }

}
</style>
